<?php

/**
 * Yii Controller ProfileController
 *
 * @author trangnguyen
 * @since May 6, 2014 - 3:27:31 PM
 */
class ProjectController extends MyController
{

    public function actionIndex()
    {
        $url = isset($_GET['url']) ? $_GET['url'] : null;
        if (!$url)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        $category = Category::model()->find('url = :u', array(':u' => $url));
        if (!$category)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        $criteria = new CDbCriteria();
        if ($category->parent_id == 0)
        {
            $criteria->condition = 'status!=:st';
            $criteria->params    = array(
                ':st' => Post::STATUS_DRAFT,
            );
            $criteria->addInCondition('c_id', Category::getSubCateById($category->id));
        }
        else
        {
            $criteria->condition = 'status!=:st and c_id=:cate';
            $criteria->params    = array(
                ':cate' => $category->id,
                ':st'   => Product::STATUS_DRAFT,
            );
        }
        $criteria->limit   = 5;
        $criteria->order   = 'status DESC , id DESC';
        $limit_index       = Option::getItem('limit_product_index') ? Option::getItem('limit_product_index') : 12;
        $count             = Product::model()->count($criteria);
        $pages             = new CPagination($count);
        $pages->route      = $this->route;
        $pages->pageSize   = $limit_index;
        $pages->applyLimit($criteria);
        //breadcrumbs
        $this->breadcrumbs = array(
            $category->name
        );
        $products          = Product::model()->findAll($criteria);
        //gan gia tri vao bien category
        $this->category    = $category->name;
        //breadcrumbs
        $this->breadcrumbs = array(
            $category->name
        );
        $this->render('index', array(
            'category' => $category,
            'products' => $products,
            'pages'    => $pages,
        ));
    }

    public function getProductByUrl()
    {
        $url = isset($_GET['url']) ? $_GET['url'] : null;

        if (!$url)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        $product = Product::model()->find('url = :u', array(':u' => $url));

        if (!$product)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $product;
    }

    public function actionView()
    {
        $this->showBtn = false;
        $product       = $this->getProductByUrl();

        Product::model()->updateByPk($product->id, array(
            'count_view' => $product->count_view + 1
        ));
        $this->category    = $product->category->name;
        $this->breadcrumbs = array(
            $product->category->name => $product->category->publicUrl,
            $product->name
        );

        $limit_box = Option::getItem('limit_project_relation') ? Option::getItem('limit_project_relation') : 10;
        $relations = Product::model()->findAll(array(
            'condition' => 'id != :i and status > :s and c_id = :c',
            'params'    => array(
                ':i' => $product->id,
                ':c' => $product->category->id,
                ':s' => Product::STATUS_DRAFT,
            ),
            'order'     => 'id DESC ',
            'limit'     => $limit_box
        ));
        $this->google_index = $product->google_index;
        $this->render('view', array(
            'product'   => $product,
            'relations' => $relations,
        ));
    }

    public function actionRegister()
    {
        $product = $this->getProductByUrl();

        $model = new Register('useCaptcha');
//        $model->setScenario('unique');
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'Register-Form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if (isset($_POST['Register']))
        {
            $model->attributes   = $_POST['Register'];
            $model->created_at   = time();
            $model->p_id         = $product->id;
            $model->name_project = $product->name;
            if ($model->validate())
            {
                if ($model->save())
                {
                    $body     = $this->renderInternal(Yii::getPathOfAlias('webroot.themes') .
                            DIRECTORY_SEPARATOR . "deltaGroup" .
                            DIRECTORY_SEPARATOR . "views" .
                            DIRECTORY_SEPARATOR . "layouts" .
                            DIRECTORY_SEPARATOR . "noti_register.php", array(
                        'model' => $model), true);
                    $sendMail = New MyEmail();
                    if (Option::getItem('mail_admin'))
                    {
                        $mail_to = explode(',', Option::getItem('mail_admin'));
                        foreach ($mail_to as $vl)
                        {
                            $sendMail->subject = 'Thông báo có "đăng ký tham quan" mới!';
                            $sendMail->to      = trim($vl);
                            $sendMail->body    = $body;
                            $sendMail->send();
                        }
                    }
                    Yii::app()->user->setFlash('contact-success', Yii::t('contact', 'Cảm ơn bạn ! Bạn đã đăng ký thành công.'));
                    $this->refresh();
                }
                else
                {
                    Yii::app()->user->setFlash('contact-err', Yii::t('contact', 'Không gửi được thông tin ! mời bạn kiểm tra lại'));
                }
            }
        }
        $this->breadcrumbs = array(
            ' Đăng ký tham quan'
        );
        $this->render('register', array(
            'product' => $product,
            'model'   => $model,
        ));
    }

    public function actionHot()
    {
        //gan gia tri vao bien category
        $this->category = 'Dự Án HOT';

        $cri               = new CDbCriteria();
        $cri->condition    = 'status > :s and type=:ty';
        $cri->params       = array(
            ':s'  => Product::STATUS_DRAFT,
            ':ty' => 2
        );
        $cri->order        = 'rank DESC , status DESC , id DESC';
        //setting limit product
        $limit_index       = Option::getItem('limit_product_index') ? Option::getItem('limit_product_index') : 12;
        $count             = Product::model()->count($cri);
        $pages             = new CPagination($count);
        $pages->route      = $this->route;
        $pages->pageSize   = $limit_index;
        $pages->applyLimit($cri);
        //breadcrumbs
        $this->breadcrumbs = array(
            'Dự Án HOT'
        );
        $products          = Product::model()->findAll($cri);
        $this->render('hot', array(
            'products' => $products,
            'pages'    => $pages,
        ));
    }

}

/** 
 * End file ProfileController
 */
